package manage.actions.menu;

import java.util.List;

import manage.actions.ManageAction;
import manage.commons.annontations.RequiredLogin;
import manage.commons.annontations.SuperManager;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.menu.model.Menu;
import manage.modules.menu.model.MenuFolders;
import manage.modules.menu.service.MenuService;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@SuperManager
public class MenuAction extends ManageAction {

	private static final long serialVersionUID = -3353379976067113360L;
	private MenuService menuService;
	private Pager<Menu> pager = new Pager<Menu>(100);
	private Menu menu;
	private List<MenuFolders> menuFolders;

	@Override
	@Action("menu-input")
	public String input() {
		getRequest().setAttribute("parentMenus",
				menuService.listAllParentMenu());
		return SUCCESS;
	}

	@Action(value = "menu-addorupdate", results = { @Result(name = SUCCESS, location = "/menu/edit-menu.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/menu/menu-add.do";
			getRequest().setAttribute("btnKeyWord", "添加菜单");
		} else if (isUpdateOperater()) {
			action = "/menu/menu-edit.do";
			getRequest().setAttribute("btnKeyWord", "更新菜单");
			menu = menuService.select(menu.getMenuId());
		}
		getRequest().setAttribute("parentMenus",
				menuService.listAllParentMenu());
		return SUCCESS;
	}

	@Action(value = "menu-query", results = { @Result(name = SUCCESS, location = "/menu/menu-list.jsp") })
	public String query() throws Exception {
		menuService.query(menu, pager);
		pager.pagerUtils(pager, getRequest());
		return SUCCESS;
	}

	@Action(value = "list-my-menu-tree", results = { @Result(name = SUCCESS, location = "/p.jsp") })
	public String listMyMenuTree() {
		request.setAttribute("menuFolders",
				menuService.listMenusByWorkerId(currentWorker.getWorkerId()));
		return SUCCESS;
	}

	@Action(value = "menu-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "menu" }) })
	public String select() {
		if (null != menu && null != menu.getMenuId()) {
			boolean exists = menuService.exists(menu.getMenuId());
			if (exists) {
				menu = menuService.select(menu.getMenuId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "menu-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String add() throws Exception {
		if (null != menu && menu.getMenuName() != null && null != menu.getUrl()) {
			int count = menuService.insert(menu);
			if (count == 1) {
				inputStream = InputStreamUtil.getSuccessInputStream();
			} else {
				inputStream = InputStreamUtil.getFailedInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "menu-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String delete() throws Exception {
		if (null != menu && null != menu.getMenuId()) {
			boolean exists = menuService.exists(menu.getMenuId());
			if (exists) {
				if (!menuService.existChildMenu(menu.getMenuId())) {
					int delNum = menuService.delete(menu.getMenuId());
					if (delNum == 1) {
						inputStream = InputStreamUtil.getSuccessInputStream();
					}
				} else {
					inputStream = InputStreamUtil
							.toInputStream("existchildmenu");
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "menu-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String edit() throws Exception {
		if (null != menu && null != menu.getMenuId()
				&& menu.getMenuName() != null && null != menu.getUrl()) {
			boolean exists = menuService.exists(menu.getMenuId());
			if (exists) {
				int count = menuService.update(menu);
				if (count >= 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getNoChangeInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		}
		return SUCCESS;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public Pager<Menu> getPager() {
		return pager;
	}

	public void setPager(Pager<Menu> pager) {
		this.pager = pager;
	}

	public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public List<MenuFolders> getMenuFolders() {
		return menuFolders;
	}

	public void setMenuFolders(List<MenuFolders> menuFolders) {
		this.menuFolders = menuFolders;
	}

}
